﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZHDJ.Data.Models.MassLine
{
    public class PartyBranchStudyActivitie:EntityBase
    {
        #region 字段属性
        /// <summary>
        /// Title
        /// </summary>
        public string Title { get; set; }

        /// <summary>
        /// 学习活动类别
        /// </summary>
        public string Category { get; set; }
        /// <summary>
        /// 学习方式 1 集中讨论式学习 2调研式学习 3小型分散式学习 4体验式学习
        /// </summary>
        public string StudyType { get; set; }

        /// <summary>
        /// ImageUrl
        /// </summary>
        public string ImageUrl { get; set; }

        /// <summary>
        /// Content
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 发布范围
        /// </summary>
        public string BranchID { get; set; }

        /// <summary>
        /// 是否同步到首页
        /// </summary>
        public int? Status { get; set; }

        /// <summary>
        /// CreatedBy
        /// </summary>
        public string CreateBy { get; set; }

        /// <summary>
        /// CreatedTime
        /// </summary>
        public DateTime? CreateTime { get; set; }

        /// <summary>
        /// ModifyBy
        /// </summary>
        public string ModifyBy { get; set; }

        /// <summary>
        /// ModifyTime
        /// </summary>
        public DateTime? ModifyTime { get; set; }

        /// <summary>
        /// 是否同步到所有组织
        /// </summary>
        public bool IsSynOrgAll { get; set; }

        /// <summary>
        /// 是否提交
        /// </summary>
        public bool IsSubmit { get; set; }

        /// <summary>
        /// 专题
        /// </summary>
        public string Thematic { get; set; }
        /// <summary>
        /// 发言对象
        /// </summary>
        public string Record { get; set; }
        /// <summary>
        /// 参与人
        /// </summary>
        public string Participant { get; set; }

        #endregion

        public static DataSet GetData(int pageIndex, int pageSize, string title, string branchId, string category)
        {
            List<MySqlParameter> parameters = new List<MySqlParameter>()
            {
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "Begin",
                    Value = (pageIndex)*pageSize
                },
                new MySqlParameter()
                {
                    DbType = DbType.Int32,
                    ParameterName = "PageSize",
                    Value = pageSize
                }
            };
            StringBuilder sbsql = new StringBuilder();
            sbsql.Append(@"SELECT SQL_CALC_FOUND_ROWS  data.*,member.UserName,branch.BranchName,IF(data.STATUS=1,'是','否') as StatusName  FROM tblPartyBranchStudyActivitie data
                           LEFT JOIN tblpartyorgmember member on Data.CreateBy=member.LoginID
                           LEFT JOIN tblpartyorgbranch branch ON Data.BranchID=branch.BranchID where 1=1 ");
            if (!string.IsNullOrEmpty(title))
            {
                sbsql.Append(" and data.Title like @Title ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "Title",
                        Value = string.Format("%{0}%", title)
                    });
            }
            if (!string.IsNullOrEmpty(branchId))
            {
                sbsql.Append(" and data.BranchID=?BranchID ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "BranchID",
                        Value = branchId
                    });
            }
            if (!string.IsNullOrEmpty(category))
            {
                sbsql.Append(" and data.category=?category ");
                parameters.Add(
                    new MySqlParameter()
                    {
                        DbType = DbType.String,
                        ParameterName = "category",
                        Value = category
                    });
            }
            sbsql.Append(" order by data.CreateTime desc LIMIT ?Begin ,?PageSize;SELECT FOUND_ROWS();");
            DataSet ds = MySqlHelper.ExecuteDataSet(sbsql.ToString(), parameters.ToArray());
            return ds;
        }
    }
}
